#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/* 确保此文件仅供编译器使用，汇编器会忽略 */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
  #include <stdint.h>
  extern uint32_t SystemCoreClock;    /* 系统主时钟频率，由系统启动或 HAL 库提供 */
#endif
#ifndef CMSIS_device_header
  #define CMSIS_device_header "stm32f4xx.h"  /* CMSIS 设备头文件 */
#endif /* CMSIS_device_header */

/*-----------------------------------------------------------
 * 调度器基本配置
 *----------------------------------------------------------*/
#define configUSE_PREEMPTION                    1   /* 1: 抢占式调度，0: 协程式调度 */
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1   /* 1: 启用硬件优化的就绪任务选择 */
#define configUSE_TICKLESS_IDLE                 0   /* 1: 启用低功耗空闲模式，停止 SysTick */
#define configCPU_CLOCK_HZ                      ( SystemCoreClock )
#define configTICK_RATE_HZ                      ( ( TickType_t ) 1000 )  /* 系统节拍频率，单位 Hz */
#define configMAX_PRIORITIES                    32  /* 最大支持的任务优先级数 */
#define configMINIMAL_STACK_SIZE                ( ( uint16_t ) 128 )    /* 空闲任务栈深度 */
#define configMAX_TASK_NAME_LEN                 16  /* 任务名最大长度 */
#define configUSE_16_BIT_TICKS                  0   /* 1: 使用 16 位 Tick 计数 */
#define configIDLE_SHOULD_YIELD                 1   /* 1: 同优先级任务可抢占空闲任务 */

/*-----------------------------------------------------------
 * 任务通知及同步机制
 *----------------------------------------------------------*/
#define configUSE_TASK_NOTIFICATIONS            1   /* 使能任务通知功能 */
#define configTASK_NOTIFICATION_ARRAY_ENTRIES   1   /* 通知数组长度 */
#define configUSE_MUTEXES                       1   /* 使能互斥锁（Mutex） */
#define configUSE_RECURSIVE_MUTEXES             1   /* 使能递归互斥锁 */
#define configUSE_COUNTING_SEMAPHORES           1   /* 使能计数信号量 */
#define configQUEUE_REGISTRY_SIZE               8   /* 队列注册表大小，用于调试 */
#define configUSE_QUEUE_SETS                    1   /* 使能队列集合 */

/*-----------------------------------------------------------
 * 调度特性与统计
 *----------------------------------------------------------*/
#define configUSE_TIME_SLICING                  1   /* 使能同优先级任务时间片轮转 */
#define configGENERATE_RUN_TIME_STATS           0   /* 1: 生成运行时间统计 */
#if ( configGENERATE_RUN_TIME_STATS == 1 )
#include "./BSP/TIMER/btim.h"
extern uint32_t FreeRTOSRunTimeTicks;
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() ConfigureTimeForRunTimeStats()
#define portGET_RUN_TIME_COUNTER_VALUE()        FreeRTOSRunTimeTicks
#endif

/*-----------------------------------------------------------
 * 内存分配配置
 *----------------------------------------------------------*/
#define configSUPPORT_STATIC_ALLOCATION         0   /* 使能静态内存分配 */
#define configSUPPORT_DYNAMIC_ALLOCATION        1   /* 使能动态内存分配 */
#define configTOTAL_HEAP_SIZE                   ( ( size_t ) ( 10 * 1024 ) ) /* 总堆大小，字节 */
#define configAPPLICATION_ALLOCATED_HEAP        0   /* 1: 应用自行定义堆数组 */

/*-----------------------------------------------------------
 * 钩子函数及错误检查
 *----------------------------------------------------------*/
#define configUSE_IDLE_HOOK                     0   /* 使能空闲钩子函数 */
#define configUSE_TICK_HOOK                     0   /* 使能节拍钩子函数 */
#define configCHECK_FOR_STACK_OVERFLOW          0   /* 栈溢出检测, 1 或 2 */
#define configUSE_MALLOC_FAILED_HOOK            0   /* 动态分配失败钩子 */
#define configUSE_DAEMON_TASK_STARTUP_HOOK      0   /* 定时器任务启动钩子 */

/*-----------------------------------------------------------
 * 协程支持（已弃用，可忽略）
 *----------------------------------------------------------*/
#define configUSE_CO_ROUTINES                   0   /* 使能协程 */
#define configMAX_CO_ROUTINE_PRIORITIES         2   /* 协程优先级数 */

/*-----------------------------------------------------------
 * 软件定时器
 *----------------------------------------------------------*/
#define configUSE_TIMERS                        1   /* 使能软件定时器 */
#define configTIMER_TASK_PRIORITY               ( configMAX_PRIORITIES - 1 ) /* 定时器任务优先级 */
#define configTIMER_QUEUE_LENGTH                5   /* 定时器队列长度 */
#define configTIMER_TASK_STACK_DEPTH            ( configMINIMAL_STACK_SIZE * 2 ) /* 定时器任务栈深度 */

/*-----------------------------------------------------------
 * 可选 API 函数
 *----------------------------------------------------------*/
#define INCLUDE_vTaskPrioritySet                1
#define INCLUDE_uxTaskPriorityGet               1
#define INCLUDE_vTaskDelete                     1
#define INCLUDE_vTaskSuspend                    1
#define INCLUDE_xResumeFromISR                  1
#define INCLUDE_vTaskDelayUntil                 1
#define INCLUDE_vTaskDelay                      1
#define INCLUDE_xTaskGetSchedulerState          1
#define INCLUDE_xTaskGetCurrentTaskHandle       1
#define INCLUDE_uxTaskGetStackHighWaterMark     1
#define INCLUDE_xTaskGetIdleTaskHandle          1
#define INCLUDE_eTaskGetState                   1
#define INCLUDE_xEventGroupSetBitFromISR        1
#define INCLUDE_xTimerPendFunctionCall          1
#define INCLUDE_xTaskAbortDelay                 1
#define INCLUDE_xTaskGetHandle                  1
#define INCLUDE_xTaskResumeFromISR              1

/*-----------------------------------------------------------
 * 中断优先级配置
 *----------------------------------------------------------*/
#ifdef __NVIC_PRIO_BITS
  #define configPRIO_BITS       __NVIC_PRIO_BITS
#else
  #define configPRIO_BITS       4   /* STM32F4 默认 4 位优先级位宽 */
#endif
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY   15  /* 最低中断优先级 */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 /* 最大可调用 FreeRTOS API 的中断优先级 */
#define configKERNEL_INTERRUPT_PRIORITY           ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
#define configMAX_SYSCALL_INTERRUPT_PRIORITY      ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
#define configMAX_API_CALL_INTERRUPT_PRIORITY     configMAX_SYSCALL_INTERRUPT_PRIORITY

/*-----------------------------------------------------------
 * FreeRTOS 中断服务函数映射 (CMSIS 风格)
 *----------------------------------------------------------*/
#define vPortSVCHandler    SVC_Handler
#define xPortPendSVHandler PendSV_Handler

/*-----------------------------------------------------------
 * 断言宏
 *----------------------------------------------------------*/
#define configASSERT( x )    if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

/*-----------------------------------------------------------
 * FPU / MPU 支持
 *----------------------------------------------------------*/
#define configENABLE_FPU                           1   /* 使能 Cortex-M4 FPU */
#define configENABLE_MPU                           0   /* 使能 MPU 支持 */

/*-----------------------------------------------------------
 * 用户定义区域
 * 可在此添加额外宏定义
 *----------------------------------------------------------*/
/* USER CODE BEGIN Defines */

/* USER CODE END Defines */

#endif /* FREERTOS_CONFIG_H */
